<?php

namespace app\api\controller\order;

use think\Controller;
use think\Request;

class Order extends Controller {
    public function __construct()
    {
        parent::__construct();
        $this->UserModel = model('User.User');
        $this->OrderModel = model('Order.Order');
        $this->OrderProductModel = model('Order.OrderProduct');
        $this->AddressModel = model('User.Address');
        $this->ExpressModel = model('Express');

    }

    // 获取订单信息
    public function orderInfo () {
        $id = $this->request->param('id');
        $userid = $this->request->param('userid');
        // halt($userid);
        $user = $this->UserModel->find($userid);

        if (!$user) {
            $this->error('用户不存在，请重试');
        }
        /**
         * 订单表
         * 订单商品表
         * 商品表
         * 配送表
         * 用户收货地址表
         */

        //  订单
        $Order = $this->OrderModel->find($id);
        // halt($Order['addrid']);
        // 配送表
        $Express = $this->ExpressModel->where('id', $Order['expid'])->find();
        // 订单商品
        $OrderProduct = $this->OrderProductModel->with('product')->where(['orderid' => $id])->select();
        // 收货地址
        $Address = $this->AddressModel->with(['provinces','citys','districts'])->find($Order['addrid']);
        // halt($OrderProduct);
        if (!$Order || !$OrderProduct || !$Address) {
            $this->error('订单不存在，请重试');
        }
        $data = [
            'Order' => $Order,
            'OrderProduct' => $OrderProduct,
            'Address' => $Address,
            'Express' => $Express
        ];
        $this->success('查询成功', null, $data);
    }

    // 获取订单状态
    public function orderList () {
        $userid = $this->request->param('userid');
        $status = $this->request->param('status');

        $where = [];
        if (!empty($userid)) {
            $where['userid'] = $userid;
        }

        if ($status != 0) {
            $where['status'] = $status;
        }

        $list = $this->OrderModel->where($where)->select();
        foreach ($list as $item) {
            $OrderProcut = $this->OrderProductModel->with('product')->where('orderid', $item['id'])->select();
            $item['OrderProduct'] = $OrderProcut;

        }
        $this->success('查询成功', null, $list);
    }

    // 确认收货
    public function receive () {
        $id = $this->request->param('id');
        $order = $this->OrderModel->find($id);
        if (!$order) {
            $this->error('订单不存在');
        }
        $OrderData = [
            'id' => $id,
            'status' => 3
        ];
        $Orderstate = $this->OrderModel->isUpdate(true)->save($OrderData);
        if ($Orderstate == false) {
            $this->error('收货失败，请稍后再试');
        } else {
            $this->success('收货成功');
        }
    }

    // 评分
    public function rate () {
        $id = $this->request->param('id');
        $rate = $this->request->param('rate');
        $Order = $this->OrderModel->find($id);
        if (!$Order) {
            $this->error('订单不存在！');
        }
        $OrderProcut = $this->OrderProductModel->where('orderid', $Order['id'])->select();
        // halt($OrderProcut['0']['id']);
        // return $OrderProcut;
        if (!$OrderProcut) {
            $this->error('订单不存在~');
        }

         // 开启事务
         $this->OrderModel->startTrans();
         $this->OrderProductModel->startTrans();

        //  修改订单数据
        $OrderData = [
            'id' => $id,
            'status' => 4
        ];
        $OrderStatus = $this->OrderModel->isUpdate(true)->save($OrderData);
        if ($OrderStatus == false) {
            $this->error('修改订单数据失败');
        }

        // 修改订单商品表数据
        $OrderProductData = [];
        foreach($OrderProcut as $item) {
            $arr = [
                'id' => $item['id'],
                'orderid' => $id,
                'rate' => $rate
            ];
            array_push($OrderProductData, $arr);
        }
        // halt($OrderProductData);
        $OrderProductStatus = $this->OrderProductModel->isUpdate(true)->saveAll($OrderProductData);
        if ($OrderProductStatus == false) {
            $this->OrderModel->rollback();
            $this->error('修改订单商品表数据失败');
        }

        if ($OrderProductStatus == false || $OrderStatus == false) {
            $this->OrderModel->rollback();
            $this->OrderProductModel->rollback();
            $this->error('评价失败');
        } else {
            $this->OrderModel->commit();
            $this->OrderProductModel->commit();
            $this->success('感谢您的评价，祝您生活愉快~', null, $rate);
        }
    }
}
